package com.mobi.Survey.lib.model;

import java.util.Calendar;

import android.os.Parcel;
import android.os.Parcelable;

public class AlarmRepeatPojo extends Pojo implements Parcelable {

	private int repeatType;
	private long id;
	private long alarmId;
	private String text;

	public AlarmRepeatPojo() {

	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getAlarmId() {
		return alarmId;
	}

	public void setAlarmId(long alarmId) {
		this.alarmId = alarmId;
	}

	public void setRepeatType(int repeatType) {
		this.repeatType = repeatType;
	}

	public void setText(String text) {
		this.text = text;
	}

	public static final AlarmRepeatPojo NO_REPEAT = new AlarmRepeatPojo(0, "No");

	public static final AlarmRepeatPojo MONDAY = new AlarmRepeatPojo(2, "Monday");
	public static final AlarmRepeatPojo TUESDAY = new AlarmRepeatPojo(3, "Tuesday");
	public static final AlarmRepeatPojo WEDNESDAY = new AlarmRepeatPojo(4, "Wednesday");
	public static final AlarmRepeatPojo THURSDAY = new AlarmRepeatPojo(5, "Thursday");
	public static final AlarmRepeatPojo FRIDAY = new AlarmRepeatPojo(6, "Friday");
	public static final AlarmRepeatPojo SATURDAY = new AlarmRepeatPojo(7, "Saturday");
	public static final AlarmRepeatPojo SUNDAY = new AlarmRepeatPojo(1, "Sunday");

	public static final AlarmRepeatPojo EVERY_DAY = new AlarmRepeatPojo(8, "Every Day");
	public static final AlarmRepeatPojo EVERY_HOUR = new AlarmRepeatPojo(9, "Every Hour");
	public static final AlarmRepeatPojo EVERY_MINUTE = new AlarmRepeatPojo(10, "Every Minute");
	public static final AlarmRepeatPojo EVERY_SECOND = new AlarmRepeatPojo(11, "Every Second");
	public static final AlarmRepeatPojo EVERY_YEAR = new AlarmRepeatPojo(12, "Every Year");
	public static final AlarmRepeatPojo EVERY_MONTH = new AlarmRepeatPojo(13, "Every Month");
	public static final AlarmRepeatPojo FIRST_WEEK_MONTH = new AlarmRepeatPojo(14, "Every first week of the month");
	public static final AlarmRepeatPojo SECOND_WEEK_MONTH = new AlarmRepeatPojo(15, "Every second week of the month");
	public static final AlarmRepeatPojo THIRD_WEEK_MONTH = new AlarmRepeatPojo(16, "Every third week of the month");
	public static final AlarmRepeatPojo FOURTH_WEEK_MONTH = new AlarmRepeatPojo(17, "Every fourth week of the month");

	private AlarmRepeatPojo(int repeatType, String text) {

		this.repeatType = repeatType;
		this.text = text;
	}

	public static final AlarmRepeatPojo getAlarmRepeat(int type) {
		switch (type) {
		case 0:
			return NO_REPEAT;

		case 1:
			return SUNDAY;
		case 2:
			return MONDAY;
		case 3:
			return TUESDAY;
		case 4:
			return WEDNESDAY;
		case 5:
			return THURSDAY;
		case 6:
			return FRIDAY;
		case 7:
			return SATURDAY;
		case 8:
			return EVERY_DAY;
		case 9:
			return EVERY_HOUR;
		case 10:
			return EVERY_MINUTE;
		case 11:
			return EVERY_SECOND;
		case 12:
			return EVERY_YEAR;
		case 13:
			return EVERY_MONTH;
		case 14:
			return FIRST_WEEK_MONTH;
		case 15:
			return SECOND_WEEK_MONTH;
		case 16:
			return THIRD_WEEK_MONTH;
		case 17:
			return FOURTH_WEEK_MONTH;

		}
		return null;
	}

	public int getRepeatType() {
		return repeatType;
	}

	public String getText() {
		return text;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {

		dest.writeInt(repeatType);
		dest.writeString(text);
	}

	public AlarmRepeatPojo(Parcel in) {
		this.repeatType = in.readInt();
		this.text = in.readString();
	}

	public static long getMilliSecondsUntil(int repeatType, long alarmStartTime, int increment) {

		final Calendar calendar = Calendar.getInstance();

		final Calendar time = Calendar.getInstance();
		final int CURRENT_HOUUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY);
		final int CURRENT_MINUTE = calendar.get(Calendar.MINUTE);

		time.setTimeInMillis(alarmStartTime);

		calendar.set(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, time.get(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, time.get(Calendar.SECOND));
		switch (repeatType) {

		case 1:
			// return SUNDAY;
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
			calendar.add(Calendar.WEEK_OF_MONTH, increment);
			break;
		case 2:
			// return MONDAY;
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			calendar.add(Calendar.WEEK_OF_MONTH, increment);
			break;
		case 3:
			// return TUESDAY;
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
			calendar.add(Calendar.WEEK_OF_MONTH, increment);
			break;
		case 4:
			// return WEDNESDAY;
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
			calendar.add(Calendar.WEEK_OF_MONTH, increment);
			break;
		case 5:
			// return THURSDAY;
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
			calendar.add(Calendar.WEEK_OF_MONTH, increment);
			break;
		case 6:
			// return FRIDAY;
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
			calendar.add(Calendar.WEEK_OF_MONTH, increment);
			break;
		case 7:
			// return SATURDAY;
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
			calendar.add(Calendar.WEEK_OF_MONTH, increment);
			break;
		case 8:
			// return EVERY_DAY;
			calendar.add(Calendar.DAY_OF_MONTH, increment);
			break;

		case 9:
			// return EVERY_HOUR;
			calendar.set(Calendar.HOUR_OF_DAY, CURRENT_HOUUR_OF_DAY);
			calendar.add(Calendar.HOUR_OF_DAY, increment);
			break;
		case 10:
			// return EVERY_MINUTE;
			calendar.set(Calendar.MINUTE, CURRENT_MINUTE);
			calendar.set(Calendar.HOUR_OF_DAY, CURRENT_HOUUR_OF_DAY);
			calendar.add(Calendar.MINUTE, increment);
			break;
		case 11:
			// return EVERY_SECOND;
			calendar.set(Calendar.MINUTE, CURRENT_MINUTE);
			calendar.set(Calendar.HOUR_OF_DAY, CURRENT_HOUUR_OF_DAY);
			calendar.add(Calendar.SECOND, increment);

			break;
		case 12:
			// return EVERY_YEAR;
			calendar.set(Calendar.MONTH, time.get(Calendar.MONTH));
			calendar.set(Calendar.DAY_OF_MONTH, time.get(Calendar.DAY_OF_MONTH));
			calendar.add(Calendar.YEAR, increment);
			break;
		case 13:
			// return EVERY_MONTH;
			calendar.set(Calendar.DAY_OF_MONTH, time.get(Calendar.DAY_OF_MONTH));

			calendar.add(Calendar.MONTH, increment);
			break;
		case 14:
			// return FIRST_WEEK_MONTH;
			calendar.set(Calendar.WEEK_OF_MONTH, 1);
			calendar.add(Calendar.MONTH, increment);
			break;

		case 15:
			// return SECOND_WEEK_MONTH;

			calendar.set(Calendar.WEEK_OF_MONTH, 2);
			calendar.add(Calendar.MONTH, increment);

			break;
		case 16:
			// return THIRD_WEEK_MONTH;
			calendar.set(Calendar.WEEK_OF_MONTH, 3);
			calendar.add(Calendar.MONTH, 1);

			break;
		case 17:
			// return FOURTH_WEEK_MONTH;
			calendar.set(Calendar.WEEK_OF_MONTH, 4);
			calendar.add(Calendar.MONTH, 1);

			break;

		}
		return calendar.getTimeInMillis();

	}

	public static final Parcelable.Creator<AlarmRepeatPojo> CREATOR = new Parcelable.Creator<AlarmRepeatPojo>() {
		public AlarmRepeatPojo createFromParcel(Parcel in) {
			return new AlarmRepeatPojo(in);
		}

		public AlarmRepeatPojo[] newArray(int size) {
			return new AlarmRepeatPojo[size];
		}
	};

}
